package com.onluy.plugins.telemanager;

import android.os.Handler;
import android.os.Message;
import android.util.Log;

import org.json.JSONObject;

import java.text.SimpleDateFormat;
import java.util.Date;

public class CellMan extends Thread {

    private String outNumber="";
    private String cellNumber="";
    private Date startDate;
    private Date endDate;
private   String receiver;
    public String getOutNumber() {
        return outNumber;
    }

    public void setOutNumber(String outNumber) {
        this.outNumber = outNumber;
    }

    public String getCellNumber() {
        if(cellNumber==null  || "".equals(cellNumber)  )cellNumber="-1";
        return cellNumber;
    }

    public void setCellNumber(String cellNumber) {
        this.cellNumber = cellNumber;
    }

    public Date getStartDate() {
        return startDate ==null?new Date():startDate;
    }

    public void setStartDate(Date startDate) {
        this.startDate = startDate;
    }

    public Date getEndDate() {
        return endDate ==null?new Date():endDate;
    }

    public void setEndDate(Date endDate) {
        this.endDate = endDate;
    }

    public long getDuration() {
        if(startDate ==null || endDate ==null)
            return   0;
        else
            return   (endDate.getTime() - startDate.getTime())/1000;
    }



    private  static CellMan _CellMan=null;
    private  CellMan (){}
    public static CellMan getInstance(){
        if(_CellMan ==null)_CellMan=new CellMan();
            return  _CellMan;
    }

    private Handler msgHandler;
    private  void  SendMsg(String msgstr){
        Log.e("===========", "SendMsg:" +1+ msgstr);
        if(msgHandler!=null) {
            Message msg = new Message();
            msg.what=1;
            msg.obj=msgstr;
            msgHandler.sendMessage(msg);//向Handler发送消息，
        }
    }

    public   void  SendCallBackMsg(){
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String msg="{\"cellNumber\":\""+getCellNumber()
                +"\",\"outNumber\":\""+getOutNumber()
                +"\",\"startDate\":\""+sdf.format(getStartDate())
                +"\",\"endDate\":\""+sdf.format(getEndDate())
                +"\",\"duration\":"+getDuration()
                +",\"receiver\":\""+getReceiver()+"\"}";
        if(getOutNumber().equals(getReceiver()))SendMsg(msg);
    }

    public void setMsgHandler(Handler msgHandler) {
        this.msgHandler = msgHandler;
    }

    public String getReceiver() {
        return receiver;
    }

    public void setReceiver(String receiver) {
        this.receiver = receiver;
    }
}
